<!-- 设备管理-设备运行管理-状态原因页面 -->
<template>
  <div class="equip-wrap-common">
    <px-form class="custom-form-ui equip-form" :model="queryParams" label-position="right" label-width="120px">
      <px-form-item label="状态原因" style="width: 400px">
        <px-input
          v-model="queryParams.value"
          placeholder="请输入状态原因"
          maxlength="50"
          clearable
          @blur="queryParams.value = $event.target.value.trim()"
          @keyup.enter="queryTable"
        >
          <template #suffix>
            <px-icon style="cursor: pointer" @click="queryTable">
              <search />
            </px-icon>
          </template>
        </px-input>
      </px-form-item>
      <px-button type="primary" @click="queryTable">查询</px-button>
      <px-button @click="resetTable">重置</px-button>
    </px-form>
    <div class="equip-table-btn">
      <px-button v-has="'ALL_EQUIP_RUNMANAGE_STATUSREASON_ADD'" type="primary" @click="addBtn">新增状态原因</px-button>
    </div>
    <px-table :data="tableData" class="equip-table" size="small" height="calc(100vh - 304px)" border>
      <px-table-column label="序号" width="80" align="center" fixed="left">
        <template #default="{ $index }">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </px-table-column>
      <px-table-column
        v-for="(item, index) in tableColumns"
        :key="index"
        :label="item.label"
        :min-width="item.minWidth"
        :align="item.align"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :fixed="item.fixed"
      >
        <template #default="scope">
          {{ scope.row[item.prop] === 0 ? scope.row[item.prop] : scope.row[item.prop] ? scope.row[item.prop] : "-" }}
        </template>
      </px-table-column>
      <px-table-column fixed="right" label="操作" width="120" align="center">
        <template #default="scope">
          <px-tooltip content="编辑" placement="top">
            <px-button v-has="'ALL_EQUIP_RUNMANAGE_STATUSREASON_EDIT'" link type="primary" icon="EditPen" @click="handleEdit(scope.row)" />
          </px-tooltip>
          <px-tooltip content="删除" placement="top">
            <px-button v-has="'ALL_EQUIP_RUNMANAGE_STATUSREASON_DELETE'" link type="danger" icon="Delete" @click="handleDelete(scope.row)" />
          </px-tooltip>
        </template>
      </px-table-column>
    </px-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      class="equip-table-pagination"
      :total="total"
      @pagination="queryTable"
    />
    <!-- 新增/修改/查看弹窗 -->
    <AddEquipDialog
      :dialogVisible="dialogVisible"
      :dialogType="dialogType"
      :dialogRow="dialogRow"
      @queryTable="queryTable"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "ALL_EQUIP_RUNMANAGE_STATUSREASON"
});
import { deleteRunStatusReason, queryRunStatusReason } from "@/api/equip/runManage";
import { PxMessage, PxMessageBox } from "px-ui";
import { onMounted, reactive, toRefs } from "vue";
import AddEquipDialog from "./components/AddEquipDialog.vue";

const state = reactive<any>({
  queryParams: {
    value: "",
    pageNum: 1,
    pageSize: 20
  },
  tableData: [],
  total: 0,
  dialogVisible: false,
  dialogType: "add",
  dialogRow: {},
  tableColumns: [
    { prop: "name", label: "状态原因", minWidth: "240", showOverflowTooltip: true },
    { prop: "downTypeName", label: "状态类型", minWidth: "240", showOverflowTooltip: true },
    { prop: "updateName", label: "更新人", minWidth: "240", showOverflowTooltip: true },
    { prop: "updateTime", label: "更新时间", minWidth: "240", showOverflowTooltip: true }
  ]
});
const { queryParams, tableData, total, dialogVisible, dialogType, dialogRow, tableColumns } = toRefs(state);

// 进入页面时触发
onMounted(() => queryTable());

// 查询状态原因列表
const queryTable = async () => {
  const params = queryParams.value;
  const data = await queryRunStatusReason(params);
  tableData.value = data?.data;
  total.value = data?.total;
};
// 重置筛选条件和状态原因列表
const resetTable = async () => {
  queryParams.value = {
    value: "",
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  };
  queryTable();
};
// 关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
};
// 新增状态原因-显示弹窗
const addBtn = () => {
  dialogType.value = "add";
  dialogVisible.value = true;
};
// 编辑状态原因-显示弹窗
const handleEdit = (row: any) => {
  dialogType.value = "edit";
  dialogRow.value = row;
  dialogVisible.value = true;
};
// 删除状态原因
const handleDelete = async (row: any) => {
  PxMessageBox.confirm(`要删除当前状态原因“${row.name}”吗？`, "提示", {
    confirmButtonText: "删除",
    confirmButtonClass: "px-button--danger",
    cancelButtonText: "取消",
    type: "warning",
    draggable: true
  })
    .then(async () => {
      const params = {
        id: row.id
      };
      await deleteRunStatusReason(params);
      PxMessage.success("删除成功");
      queryTable();
    })
    .catch(() => {});
};
</script>

<style lang="scss" scoped>
@import url("@/views/all/equip/scss/table-list.scss");

.equip-wrap-common {
  height: calc(100vh - 177px);
}
</style>
